import {HYEventStore} from 'hy-event-store'
import {
  getMusicDetail,
  getMusicLyric
} from '../services/music-api'
import {parseLyric} from '../utils/handle-lyric'
const audioContext = wx.getBackgroundAudioManager()

const musicStore = new HYEventStore({
  state:{
    id:0,
    songInfo:{},
    currentTime:0,
    isPlaying:true,
    musicList:new Set(),
    currentIndex:0,
    lyrics:[],
    currentLyricIndex:0
  },
  actions:{
    playMusicWithIdAction(ctx,id){
      ctx.id = id
      if(!ctx.musicList.has(id)){
        ctx.musicList = ctx.musicList.add(id)
        ctx.currentIndex = ctx.musicList.size - 1
      }else{
        //老歌
        [...ctx.musicList].forEach((item,index) => {
          if(item === id){
            ctx.currentIndex = index
          }
        })
      }
      getMusicLyric(id).then(res => {
        const lyrics = parseLyric(res.lrc.lyric)
        ctx.lyrics = lyrics
      })

      getMusicDetail(id).then(res => {
        ctx.songInfo = res.songs[0]
        audioContext.title = res.songs[0].name
        audioContext.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`

        audioContext.onTimeUpdate(() => {
          const currentTime = audioContext.currentTime * 1000
          ctx.currentTime = currentTime
          if(!ctx.lyrics.length) return 
          let i = 0
          for(;i<ctx.lyrics.length;i++){
            if(currentTime < ctx.lyrics[i].time){
              break
            }
          }
          const currentLyricIndex = i - 1
          if(ctx.currentLyricIndex !== currentLyricIndex){
            ctx.currentLyricIndex = currentLyricIndex
          }

        })

        audioContext.onEnded(() => {
          this.dispatch('changeNewMusicAction')
        })

        audioContext.onPlay(() => {
          ctx.isPlaying = true
        })

        audioContext.onPause(() => {
          ctx.isPlaying = false
        })
        

      })
    },
    changeMusicStatusAction(ctx,isPlaying){
      ctx.isPlaying = isPlaying
      isPlaying ? audioContext.play() : audioContext.pause()
    },
    changeMusicCurrentTimeAction(ctx,value){
      const currentTime = ctx.songInfo.dt * (value / 100)
      ctx.currentTime = currentTime
      audioContext.seek(currentTime / 1000)
    },
    playAllMusicAction(ctx,musicList){
      const newMusicList = new Set(musicList)
      ctx.musicList = newMusicList
      ctx.currentIndex = 0
      this.dispatch('playMusicWithIdAction',[...newMusicList][0])
    },
    changeNewMusicAction(ctx,isNext = true){
      if(isNext){
        const currentIndex = (ctx.currentIndex + 1) % ctx.musicList.size
        ctx.currentIndex = currentIndex
        this.dispatch('playMusicWithIdAction',[...ctx.musicList][currentIndex])
      }else{
        let currentIndex = ctx.currentIndex - 1
        if(currentIndex < 0){
          currentIndex = ctx.musicList.size - 1
        }
        ctx.currentIndex = currentIndex
        this.dispatch('playMusicWithIdAction',[...ctx.musicList][currentIndex])
      }
    }
  }
})

export {
  musicStore
}